package com.shyt.api;


import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.shyt.dao.SysDepartmentRequestVo;
import com.shyt.dao.SysDepartmentVo;
import com.shyt.dao.SysUserVo;
import com.shyt.filter.annotation.RecordLogsAspectAt;
import com.shyt.impl.SysDepartmentImpl;
import com.shyt.util.ApiResponse;
import com.shyt.util.CommonUtil;
import com.shyt.util.MenuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("api/")
public class SysDepartmentApi {
    @Autowired
    SysDepartmentImpl sysDepartmentImpl;

    @RecordLogsAspectAt
    @PostMapping("getDepartmentTree")
    public ApiResponse getDepartmentTree() throws Exception {
        Authentication authentication =
                SecurityContextHolder.getContext().getAuthentication();
        if (null == authentication) {
            throw new Exception("用户信息为空，用户信息有误，请联系管理员");
        }
        SysUserVo sysUserVo = (SysUserVo) authentication.getPrincipal();
        if (null == sysUserVo) {
            return null;
        }

        List<SysDepartmentVo> sysDepartmentVos =
                sysDepartmentImpl.getLoginDepartments(sysUserVo.getId());


        sysDepartmentVos = MenuUtil.getDepartmentTree(sysDepartmentVos);

        //return new ApiResponse("200", "调用成功", sysDepartmentVos);
        return ApiResponse.ok(sysDepartmentVos);
    }

    /***
     * 根据查询条件取查询部门
     * @param sysDepartmentRequestVo
     * @return
     * @throws Exception
     */
    @RecordLogsAspectAt
    @PostMapping("getDepartments")
    public ApiResponse getDepartments(@RequestBody SysDepartmentRequestVo sysDepartmentRequestVo) throws Exception {


        List<SysDepartmentVo> sysDepartmentVos =
                sysDepartmentImpl.getDepartments(sysDepartmentRequestVo.getDepartmentName());
        sysDepartmentVos = MenuUtil.getDepartmentTree(sysDepartmentVos);
        //return new ApiResponse("200", "调用成功", sysDepartmentVos);
        return ApiResponse.ok(sysDepartmentVos);
    }

    /***
     * 根据查询条件取查询部门
     * @param sysDepartmentRequestVo
     * @return
     * @throws Exception
     */
    @RecordLogsAspectAt
    @PostMapping("deleteDepartment")
    public ApiResponse deleteDepartment(@RequestBody SysDepartmentRequestVo sysDepartmentRequestVo) throws Exception {
        sysDepartmentImpl.deleteDepartment(sysDepartmentRequestVo);
        return ApiResponse.ok("调用成功");
    }

    /***
     * 选择下拉框部门
     * @return
     * @throws Exception
     */
    @RecordLogsAspectAt
    @PostMapping("getDepartmentXlk")
    public ApiResponse getDepartmentXlk() throws Exception {
        Authentication authentication =
                SecurityContextHolder.getContext().getAuthentication();
        if (null == authentication) {
            throw new Exception("用户信息为空，用户信息有误，请联系管理员");
        }
        SysUserVo sysUserVo = (SysUserVo) authentication.getPrincipal();
        if (null == sysUserVo) {
            return null;
        }

        List<SysDepartmentVo> sysDepartmentVos =
                sysDepartmentImpl.getDepartmentXlk(sysUserVo.getId());
        List<Map> rtnList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sysDepartmentVos)) {
            sysDepartmentVos.stream().forEach(item -> {
                Map<String, String> rtnMap = new HashMap<>();
                rtnMap.put("value", item.getId());
                rtnMap.put("label", item.getDepartmentName());
                rtnList.add(rtnMap);
            });
        }

        //return new ApiResponse("200", "调用成功", rtnList);
        return ApiResponse.ok(rtnList);
    }


    /***
     * 新总部门
     * @param sysDepartmentRequestVo
     * @return
     * @throws Exception
     */
    @RecordLogsAspectAt
    @PostMapping("addDepartment")
    public ApiResponse addDepartment(@RequestBody SysDepartmentRequestVo sysDepartmentRequestVo) throws Exception {
        sysDepartmentRequestVo.setId(UUID.randomUUID().toString() + CommonUtil.sdf20.format(new Date()));

//        if (!StringUtils.isEmpty(sysDepartmentRequestVo.getPid())) {
//            List<SysDepartmentVo> sysDepartmentVos =
//                    sysDepartmentImpl.selectDepartmentById(sysDepartmentRequestVo);
//            if (!CollectionUtils.isEmpty(sysDepartmentVos)) {
//                sysDepartmentRequestVo.setParentName(sysDepartmentVos.get(0).getDepartmentName());
//            }
//        }

        sysDepartmentImpl.addDepartment(sysDepartmentRequestVo);
        //return new ApiResponse("200", "调用成功", null);

        return ApiResponse.ok("");
    }

    /***
     * 新总部门
     * @param sysDepartmentRequestVo
     * @return
     * @throws Exception
     */
    @RecordLogsAspectAt
    @PostMapping("editDepartment")
    public ApiResponse editDepartment(@RequestBody SysDepartmentRequestVo sysDepartmentRequestVo) throws Exception {
        Assert.notEmpty(sysDepartmentRequestVo.getId(), "部门号不能为空");
        sysDepartmentImpl.editDepartment(sysDepartmentRequestVo);
        //return new ApiResponse("200", "调用成功", null);
        return ApiResponse.ok("");
    }
}
